package dao

import (
	"fmt"
	"gopkg.in/mgo.v2"
)

const (
	// DBNAME is boss db
	DBNAME = "BOSS" 
	// DBURL is db path
	DBURL  = "127.0.0.1:27017"
)

var (
	session *mgo.Session
)

func getSession() *mgo.Session {
	if session == nil {
		var err error
		session, err = mgo.Dial(DBURL)
		if err != nil {
			panic(err)
		}
	}
	return session.Clone()
}

// Execute is
func Execute(col string, f func(*mgo.Collection) error) error {
	session := getSession()
	defer session.Close()
	c := session.DB(DBNAME).C(col)
	fmt.Println(c)
	return f(c)
}
